using System;
using CQRS.Events;
using CQRS.Reports;
using CQRS.Sample.Domain.Events;
using CQRS.Sample.Domain.ReportDtos;

namespace CQRS.Sample.Domain.EventHandlers
{
    public class PlaneCreatedEventHandler : IEventHandler<PlaneCreatedEvent>
    {
        private readonly IReportingRepository _reportingRepository;

        public PlaneCreatedEventHandler(IReportingRepository reportingRepository)
        {
            _reportingRepository = reportingRepository;
        }

        #region IEventHandler<PlaneCreatedEvent> Members

        public void Execute(PlaneCreatedEvent theEvent)
        {
            var planeReportsId = theEvent.PlaneId;

            var plane = new PlaneReport(theEvent.PlaneName, theEvent.Manufacturer, planeReportsId);
            var planeDetails = new PlaneDetailsReport(theEvent.PlaneName, theEvent.Manufacturer,
                theEvent.YearManufactured, theEvent.FlightsMadeByPlane, theEvent.PilotName, theEvent.PilotSurname,
                theEvent.FlightsMadeByPilot, planeReportsId);
            var planeHistoryReport = new PlaneHistoryReport(planeReportsId);
            _reportingRepository.Save(plane);
            _reportingRepository.Save(planeDetails);
            _reportingRepository.Save(planeHistoryReport);
        }

        #endregion
    }
}